home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / init.c < prev    next >
C/C++ Source or Header  |  1991-08-14  |  2KB  |  80 lines

  1. /* 
  2.  *  init.cc:          Initialization and Finalization Code for Shared
  3.  *                    Libraries.
  4.  *  Author:           James Kempf
  5.  *  Created On:       Fri Mar 29 14:01:48 1991
  6.  *  Last Modified By: James Kempf
  7.  *  Last Modified On: Wed Apr 24 10:34:21 1991
  8.  *  Update Count:     7
  9.  */
  10.  
  11. #include "dldefs.h"
  12.  
  13.  
  14. /*************************************************************************
  15.  *              void INIT_FUN()
  16.  *              void FINI_FUN()
  17.  * Shared libraries should link a copy of this module into them. The
  18.  * dynamic initialization/finalization function can look up these
  19.  * functions and call them to do static ctor/dtor work. Also, the
  20.  * main uses these for statically linked programs.
  21.  ************************************************************************/
  22.  
  23.  
  24. static void __do_global_fn( int * );
  25.  
  26. /* 
  27.  * Semaphore for indicating whether the library has already been initialized. 
  28.  */
  29. static int initialized = 0;
  30.  
  31. /*
  32.  * We do this, because init.c, in addition to being compiled standalone, 
  33.  * is included in drt0.cc. 
  34.  */
  35. extern int * __function_list_addr;
  36.  
  37. void INIT_FUN()
  38. {
  39.     if ( __function_list_addr && ! initialized ) {
  40.     initialized = 1;
  41.     __do_global_fn( __function_list_addr);
  42.     }
  43. }
  44.  
  45. void FINI_FUN()
  46. {
  47.     if ( __function_list_addr && initialized ) {
  48.     initialized = 0;
  49.     __do_global_fn( __function_list_addr );
  50.     }
  51. }
  52.  
  53. inline static
  54. int * _do_local_fn( int * off_ptr)
  55. {
  56.   void_fn fn;
  57.   int i;
  58.  
  59.   i = *off_ptr;
  60.   off_ptr++;
  61.  
  62.   while( --i >= 0) {
  63.     fn = (void_fn)(((int)off_ptr) - ((int)(*off_ptr)));
  64.     fn();
  65.     off_ptr++;
  66.   }
  67.  
  68.   return (* off_ptr ? (int*) *off_ptr : off_ptr);
  69. }
  70.  
  71. static
  72. void __do_global_fn( int * ip)
  73. {
  74.  
  75.   while( *ip > 0) {
  76.     ip = _do_local_fn(ip);
  77.   }
  78.   
  79. }
  80.